const app = getApp()
//var li=[];
var index = 0;
var li = [];

Page({
  data: {
    list: li,
  },
  /*addAddre:function(e){
      wx.navigateTo({
         url: '../newAddre/newAddre'
      })
    },*/
  toModifyAddre: function (e) {
    console.log("选中的电话" + e.currentTarget.dataset.addrevalue);
    console.log("选中的index" + e.currentTarget.dataset.index)
    wx.navigateTo({
      url: '../modifyAddre/modifyAddre?name=' + e.currentTarget.dataset.name + "&tel=" + e.currentTarget.dataset.tel + "&addrevalue=" + e.currentTarget.dataset.addrevalue + "&areavalue=" + e.currentTarget.dataset.areavalue + "&door=" + e.currentTarget.dataset.door + "&index=" + e.currentTarget.dataset.index
    })
  },
  toCleanOrder: function (e) {
    wx.setStorageSync("addressId", e.currentTarget.dataset.addressid);
    wx.setStorageSync("addressUserName", e.currentTarget.dataset.addressusername);
    wx.setStorageSync("addressBuyerPhone", e.currentTarget.dataset.addressbuyerphone);
    wx.setStorageSync("addressArea", e.currentTarget.dataset.addressarea);
    wx.setStorageSync("addressDetail", e.currentTarget.dataset.addressdetail);

    wx.setStorageSync("flag", true);
    wx.navigateTo({
      url: '../../test/test'
    });
    console.log("编号为：" + e.currentTarget.dataset.addressid + "，姓名为：" + e.currentTarget.dataset.addressusername + "，手机是：" + e.currentTarget.dataset.addressbuyerphone + "，地区是：" + e.currentTarget.dataset.addressarea + "，详细地址是：" + e.currentTarget.dataset.addressdetail);
    /*let pages = getCurrentPages();
    console.log("当前页面数据" + pages);
    let currPage = pages[pages.length - 1];   //当前页面
    let prevPage = pages[pages.length - 2];  //上一个页面

    prevPage.setData({// 这里是需要传递的参数值
      addressId: e.currentTarget.dataset.addressid,
      addressUserName: e.currentTarget.dataset.addressusername,
      addressBuyerPhone: e.currentTarget.dataset.addressbuyerphone,
      addressArea: e.currentTarget.dataset.addressarea,
      addressDetail: e.currentTarget.dataset.addressdetail,
    })
  

	wx.navigateBack({
    delta: 1
  })*/


    /* for(var i = 0;i<this.data.list.length;i++){
       if(i==e.currentTarget.dataset.index){
     li[e.currentTarget.dataset.index].image = "../../images/check.jpg"}
     else{
       li[i].image = "../../images/uncheck.png"
     }
   }
     wx.navigateTo({
       url: '../../test/test?addressid=' + e.currentTarget.dataset.addressid+'&addressusername=' + e.currentTarget.dataset.addressusername + "&addressbuyerphone=" + e.currentTarget.dataset.addressbuyerphone + "&addressarea=" + e.currentTarget.dataset.addressarea + "&addressdetail=" + e.currentTarget.dataset.addressdetail+"&flag="+true
         });
     console.log("编号为："+e.currentTarget.dataset.addressid+"，姓名为：" + e.currentTarget.dataset.addressusername + "，手机是：" + e.currentTarget.dataset.addressbuyerphone + "，地区是：" + e.currentTarget.dataset.addressarea + "，详细地址是：" + e.currentTarget.dataset.addressdetail);*/
  },

  onShow: function (options) {
    var that = this
    //页面初始化，option为页面跳转所带来的参数
    let openid = wx.getStorageSync("openid");
    //var openid = options.openid
    console.log("openid", openid)
    wx.request({
      url: app.globalData.baseUrl +'/userinfo/getuserinfobyopenid',
      method: 'GET',
      data: { "buyerOpenid": openid },//传入的参数
      success: function (res) {
        var list = res.data.userAddressinfo;
        if (list == null) {
          var toastText = '获取数据失败' + res.data.errMsg;
          wx.showToast({
            title: toastText,
            icon: '',
            duration: 2000
          });
        } else {
          that.setData({
            userAddressinfo: list
          });
        }
      }
    })
  },
  addAddress: function (e) {
    var length = e.currentTarget.dataset.length
    if (length<=7){
    wx.navigateTo({
      url: '../../address/editAddre/editAddre',
    })
  }else{
      wx.showToast({
        title: '限定8个地址',
        icon: '',
        duration: 2000
      });
  }
  },

  onLoad: function (options) {
    var flag = false;//判断是从哪个页面跳转过来
    var sign = 0//判断从修改页面中的保存还是删除按钮过来，保存为1，删除为2
    flag = options.flag;
    sign = options.sign;
    if (flag) {
      li.push({
        "index": index++,
        "addressusername": options.addressuser.ame,
        "addressbuyerphone": options.addressbuyerphone,
        "addressarea": options.addressarea,
        "addressdetail": options.addressdetail,
        "addrevalue": options.addrevalue,
      })
      this.setData({
        list: li
      })
    };
    if (sign == '1') {
      console.log("我是从修改页面过来的" + options.addrevalue)
      li[options.index].addressusername = options.addressUserName;
      li[options.index].addressbuyerphone = options.addressBuyerPhone;
      li[options.index].addressarea = options.addressArea;
      li[options.index].addressdetail = options.addressDetail;
      li[options.index].addrevalue = options.addrevalue;

      this.setData({
        list: li
      })
    };
    if (sign == '2') {
      li.splice(options.index, 1);
      this.setData({
        list: li
      })
    }
  },


  deleteAddress: function (e) {
    var that = this;
    console.log("addressId:" + e.currentTarget.dataset.addressid)
    wx.showModal({
      title: '提示',
      content: '确定要删除该地址吗？',

      success: function (sm) {
        if (sm.confirm) {
          wx.request({
            url: app.globalData.baseUrl +'/useraddressinfo/removeuseraddress',
            data: { "addressId": e.currentTarget.dataset.addressid },

            method: 'GET',
            success: function (res) {
              var result = res.data.success;
              var toastText = "删除成功！";
              if (result != true) {
                toastText = "删除失败";
              } else {
                that.data.userAddressinfo.splice(e.target.dataset.index, 1)
                that.setData({
                  userAddressinfo: that.data.userAddressinfo
                });
              }
              wx.showToast({
                title: toastText,
                icon: '',
                duration: 2000
              });
            }
          })
        }
      }
    })
  },
  // 下拉刷新
  onPullDownRefresh: function () {
    // 显示顶部刷新图标
    wx.showNavigationBarLoading();
    var that = this;
    that.setData({
      userAddressinfo: null
    });
    setTimeout(function () {
      that.onShow()
      // 隐藏导航栏加载框
      wx.hideNavigationBarLoading();
      // 停止下拉动作
      wx.stopPullDownRefresh();
    }, 1500);

  },

})